/**
 * 
 */
package com.nsoft.server.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.nsoft.server.master.beans.PartyMaster;
import com.nsoft.server.master.beans.VehicleMaster;
import com.nsoft.server.service.dao.PartyDao;
import com.nsoft.server.service.dao.VechileDao;
import com.nsoft.server.service.main.VechileService;
import com.nsoft.thickclient.beanUtils.BeanUtilities;
import com.nsoft.thickclient.trackMove.intr.party.Party;
import com.nsoft.thickclient.trackMove.intr.vehicle.ThinVehicle;
import com.nsoft.thickclient.trackMove.intr.vehicle.Vehicle;

/**
 * @author venkats
 * 
 */
public class VechileServiceImpl implements VechileService {

	public VechileDao getVechileDao() {
		return vechileDao;
	}

	public void setVechileDao(VechileDao vechileDao) {
		this.vechileDao = vechileDao;
	}

	public VechileDao vechileDao;
	public PartyDao partyDao;

	public PartyDao getPartyDao() {
		return partyDao;
	}

	public void setPartyDao(PartyDao partyDao) {
		this.partyDao = partyDao;
	}

	@Transactional
	public Vehicle saveVechile(Vehicle vehicle) {
		VehicleMaster vehicleMaster = null;
		// VehicleMasterPK masterPK = null;
		if (vehicle.getId() == null) {
			vehicleMaster = new VehicleMaster();
			// masterPK = new VehicleMasterPK();
		} else {
			vehicleMaster = vechileDao.findById(new Integer(vehicle.getId()
					.intValue()), false);
			// masterPK = vehicleMaster.getPk();
		}
		String[] sourceNames = { "registrationNo" };
		String[] destinationNames = { "vehCharRegno" };
		BeanUtilities.copyProerties(vehicle, vehicleMaster, sourceNames,
				destinationNames);
		// vehicleMaster.setPk(masterPK);

		sourceNames = new String[] { "make", "owner", "model",
				"registrationDate", "tonnage", "insurence",
				"insurenceExpiryDate", "lastDriverName", "lastDriverLicence",
				"remarks" };
		destinationNames = new String[] { "vehCharMake", "vehCharOwner",
				"vehCharModel", "vehDateRegdate", "vehNumTonnage",
				"vehCharInsurance", "vehDateInsurexpiry", "vehCharLastdriver",
				"vehCharDriverlicense", "vehCharGeneral1" };
		BeanUtilities.copyProerties(vehicle, vehicleMaster, sourceNames,
				destinationNames);
		PartyMaster partyMaster = partyDao.findById(new Integer(vehicle
				.getBillingPartyId().intValue()), false);
		vehicleMaster.setParty(partyMaster);
		vechileDao.makePersistent(vehicleMaster);
		return vehicle;
	}

	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public VehicleMaster getPartydetails(Long id) {
		return vechileDao.findById(new Integer(id.intValue()), false);
	}

	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<ThinVehicle> getAllThinVechile(ThinVehicle thinVehicle) {
		return vechileDao.getAllThinVechile(thinVehicle);
	}

	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<Vehicle> searchVechiles(Vehicle vehicle) {
		List<VehicleMaster> list = vechileDao.searchVechiles(vehicle);
		List<Vehicle> partyList = new ArrayList<Vehicle>();
		String[] sourceNames = new String[] { "vehCharRegno","vehCharMake", "vehCharOwner",
				"vehCharModel", "vehDateRegdate", "vehNumTonnage",
				"vehCharInsurance", "vehDateInsurexpiry", "vehCharLastdriver",
				"vehCharDriverlicense", "vehCharGeneral1" };
		String[] destinationNames = new String[] {"registrationNo", "make", "owner", "model",
				"registrationDate", "tonnage", "insurence",
				"insurenceExpiryDate", "lastDriverName", "lastDriverLicence",
				"remarks" };

		for (VehicleMaster vehicleMaster : list) {
			Vehicle party2 = new Vehicle();
			party2.setId(new Long(vehicleMaster.getVehNumId()));
			BeanUtilities.copyProerties(vehicleMaster, party2, sourceNames,
					destinationNames);
			party2.setBillingPartyId(new Long(vehicleMaster.getParty().getPartyNumId()));
			partyList.add(party2);
		}
		return partyList;
	}

}
